﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Helpers;

namespace LeadVinaglue.Repository
{
    public class ProductRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();


        #region Reference function

        public Product GetSingle(int id)
        {
            return (from _object in entities.Products where _object.ID == id select _object).FirstOrDefault();
        }
        
        public void Create([Bind(Exclude = "ID, DateCreated")] Product _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToProducts(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Products where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void UpdateReview(Product _object)
        {
            //_object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }

        public void UpdateReview(long id, int reviewed)
        {
            string sqlEx = string.Format("UPDATE [Product] SET [Reviewed]={0} WHERE [ID]={1}", reviewed, id);
            try
            {
                // Execute a query
                int returnInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringProfile, CommandType.Text, sqlEx);
            }
            catch
            {
            }
        }

        public void Update(Product _object)
        {
            //_object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }

        public void Approved(int id, int active)
        {
            string sqlEx = string.Format("UPDATE [Product] SET [Active]={0} WHERE [ID]={1}", active, id);
            try
            {
                // Execute a query
                int returnInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringProfile, CommandType.Text, sqlEx);
            }
            catch
            {
            }
        }

        public void Save([Bind(Exclude = "ID")] Product _object)
        {
            _object.EntityKey = (from Product ac in entities.Products where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }

        #endregion
    }
}
